package com.jiaai.web.xiaoke.trade;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jiaai.auth.model.AccessUser;
import com.jiaai.exception.ServerException;
import com.jiaai.trade.api.TTradeRoleApi;
import com.jiaai.trade.model.TTradeRoleVO;
import com.jiaai.util.RequestUtils;
import com.jiaai.util.RestResultGenerator;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * TODO
 * </p>
 *
 * @author 佳爱网络科技
 * @version V1.0
 * @title appTradeRoleController
 * @since 2017/12/27
 */
@RestController
@RequestMapping("/api/v1/app/tradeRole")
public class appTradeRoleController {
    @Reference
    private TTradeRoleApi tTradeRoleApi;
    /**
     * 删除单个公会下的单个成员
     * @param id
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    public Object delete(Integer tradeId,Integer id) throws ServerException {
        AccessUser accessUser = RequestUtils.getUser();
        try {
//            ((TTradeRoleVO)tTradeRoleApi.findById(tradeId,accessUser.getUserId())).getRoleName()==1
            if(4==4){
                tTradeRoleApi.delete(id,tradeId);
                return RestResultGenerator.genSuccessResult("删除成功");
            } else {
                throw new ServerException("暂无权限");
            }
        } catch (Exception e) {
            throw new ServerException("删除失败");
        }
    }
    /**
     * 添加成员
     * @param tradeId
     * @return
     * @throws Exception
     */
    @PostMapping("/joinTrade")
    public Object joinTrade(Integer tradeId,boolean isAccaus) throws ServerException {
        AccessUser accessUser = RequestUtils.getUser();
        try {
            tTradeRoleApi.joinTrade(tradeId,accessUser.getUserId(),isAccaus);
            return RestResultGenerator.genSuccessResult("添加成功");
        } catch (Exception e) {
            throw new ServerException("添加失败");
        }
    }
    /**
     * 修改成员信息
     * @param id,tradeId,mId,roleName
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    public Object update(Integer id,Integer tradeId,Integer roleName,String mId) throws ServerException {
        AccessUser accessUser = RequestUtils.getUser();
        try {
//            ((TTradeRoleVO)tTradeRoleApi.findById(tradeId,accessUser.getUserId())).getRoleName()==1
            if(2==2){
                tTradeRoleApi.update(id,tradeId,roleName,mId);
                return RestResultGenerator.genSuccessResult("修改成功");
            } else {
                throw new ServerException("暂无权限");
            }

        } catch (Exception e) {
            throw new ServerException("修改失败");
        }
    }
    /**
     * 修改申请成员状态
     * @param id
     * @param applyState
     * @return
     * @throws Exception
     */
    @PostMapping("/changeState")
    @ResponseBody
    public Object changeState(Integer id,Integer applyState,Integer tradeId,String mId) throws ServerException {
        AccessUser accessUser = RequestUtils.getUser();
        try {
//            ((TTradeRoleVO)tTradeRoleApi.findById(tradeId,accessUser.getUserId())).getRoleName()==1
            if(3==3){
                tTradeRoleApi.changeState(id,applyState,tradeId,mId);
                return RestResultGenerator.genSuccessResult("修改成功");
            } else {
                throw new ServerException("暂无权限");
            }
        } catch (Exception e) {
            throw new ServerException("修改失敗");
        }
    }

    /**
     * 获取单个公会的所有成员
     * @param curr
     * @param tradeRoleVO
     * @return
     * @throws Exception
     */
    @GetMapping("/allrole")
    @ResponseBody
    public Object allrole(@RequestParam(defaultValue = "0", required = false) Integer curr, TTradeRoleVO tradeRoleVO) throws Exception {
        return RestResultGenerator.genSuccessResult(tTradeRoleApi.allrole(curr, tradeRoleVO));
    }

}
